<template>
  <div class="component-upload-image">
    <!-- <el-upload
      class="avatar-uploader"
      :action="uploadVideoUrl"
      v-bind:data="{ FoldPath: '上传目录', SecretKey: '安全验证' }"
      v-bind:on-progress="uploadVideoProcess"
      v-bind:on-success="handleVideoSuccess"
      v-bind:before-upload="beforeUploadVideo"
      v-bind:show-file-list="false"
    >
      <video
        v-if="videoForm.showVideoPath != '' && !videoFlag"
        v-bind:src="videoForm.showVideoPath"
        class="avatar video-avatar"
        controls="controls"
      >
        您的浏览器不支持视频播放
      </video>
      <i
        v-else-if="videoForm.showVideoPath == '' && !videoFlag"
        class="el-icon-plus avatar-uploader-icon"
      ></i>
      <el-progress
        v-if="videoFlag == true"
        type="circle"
        v-bind:percentage="videoUploadPercent"
        style="margin-top: 7px"
      ></el-progress>
    </el-upload> -->
    <!-- list-type="picture-card" -->
    <el-upload
      :action="uploadVideoUrl"
      list-type="picture-card"
      :on-success="handleUploadSuccess"
      :before-upload="handleBeforeUpload"
      :on-error="handleUploadError"
      name="file"
      :show-file-list="false"
      :headers="headers"
      style="display: inline-block; vertical-align: top"
    >
      <el-image v-if="!value" :src="value">
        <div slot="error" class="image-slot">
          <i class="el-icon-plus" />
        </div>
      </el-image>
      <div v-else class="image">
        <video
          :src="value"
          :style="`width:150px;height:150px;`"
          fit="fill"
        ></video>
        <!-- <el-image
          :src="value"
          :style="`width:150px;height:150px;`"
          fit="fill"
        /> -->
        <div class="mask">
          <div class="actions">
            <span title="预览" @click.stop="dialogVisible = true">
              <i class="el-icon-zoom-in" />
            </span>
            <span title="移除" @click.stop="removeImage">
              <i class="el-icon-delete" />
            </span>
          </div>
        </div>
      </div>
    </el-upload>
    <el-dialog
      :visible.sync="dialogVisible"
      title="预览"
      width="800"
      append-to-body
    >
      <video
        autoplay
        controls
        :src="value"
        style="display: block; max-width: 100%; margin: 0 auto"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";

export default {
  data() {
    return {
      dialogVisible: false,
      uploadVideoUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
    };
  },
  props: {
    value: {
      type: String,
      default: "",
    },
  },
  methods: {
    removeImage() {
      this.$emit("input", "");
    },
    handleUploadSuccess(res) {
      console.log(res);
      this.$emit("input", res.url);
      this.loading.close();
    },
    handleBeforeUpload(file) {
      console.log(file);
      if (file.size > 50 * 1024 * 1024) {
        this.$message.error("视频大小不得超过50M");
        return false;
      }
      this.loading = this.$loading({
        lock: true,
        text: "上传中",
        background: "rgba(0, 0, 0, 0.7)",
      });
    },
    handleUploadError() {
      this.$message({
        type: "error",
        message: "上传失败",
      });
      this.loading.close();
    },
  },
  watch: {},
};
</script>

<style scoped lang="scss">
.image {
  position: relative;
  video {
    width: 300px;
    height: 300px;
  }
  .mask {
    opacity: 0;
    position: absolute;
    top: 0;
    width: 100%;
    height: 300px;
    background-color: rgba(0, 0, 0, 0.5);
    transition: all 0.3s;
    font-size: 30px;
    line-height: 300px;
  }
  &:hover .mask {
    opacity: 1;
  }
}
</style>